package controllers;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import models.DrCourt;
import models.DrMicrophone;
import play.libs.Codec;
import services.DrCourtService;
import services.DrMicroService;
import vos.DrCourtVo;
import vos.Node;
import vos.ResultVo;
import vos.ReturnCode;
import vos.ReturnListVo;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;

import constant.DialogMode;

public class DrCourtController extends BaseController {
	/**
	 * 获取下拉框
	 */
	public static void getCombobox(){
		List<Node> nodes=DrCourtService.getCombobox();
		renderJSON(nodes);
	}
	
	public static void courtManage(){
		render();
	}
	
	public static void findCourt(int page,int rows,String sort,String order,DrCourtVo drCourtVo){//ReturnListVo
		PageInfo<DrCourt> list = DrCourtService.findCourt(page,rows,sort,order,drCourtVo);
		for(DrCourt count :list.getList()){
			int phones=DrMicroService.countByCourt(count.getCourtid());
			count.setMicroCount(phones);
		}
		renderJSON(new ReturnListVo(list.getTotal(),list.getList()));
	}
	
	public static void maintianCourtDlg(String mode,String courtid){
		if(DialogMode.MODE_UPDATE.equals(mode)){
			DrCourt court = DrCourtService.getById(courtid);
			List<DrMicrophone> phones=DrMicroService.getMicroByCourt(courtid);
			renderArgs.put("phones", phones);
			renderArgs.put("drCourt", court);
		}
		render();
	}
	
	public static void courtViewDlg(String courtid){
		DrCourt court = DrCourtService.getById(courtid);
		//court.setMicroPhones();
		renderArgs.put("drCourt", court);
		render();
	}
	/**
	 * 启用
	 * @param ids
	 */
	public static void betchEnable(String ids){
		List<DrCourt> cournts=DrCourtService.getInUseCournt(ids);
		if(cournts!=null&&cournts.size()>0){
			renderRespondJson(ReturnCode.ErrorCode,"记录中包含使用中的信息");
		}
		Boolean flag = DrCourtService.betchUpdateStatus(ids,DrCourt.ENABLE,getLoginUser(),request.remoteAddress);
		if(flag)
			renderRespondJson(ReturnCode.SuccessCode,"启用成功");
		renderRespondJson(ReturnCode.ErrorCode,"启用失败");
	}
	/**
	 * 停用
	 * @param ids
	 */
	public static void betchDisable(String ids){
		List<DrCourt> cournts=DrCourtService.getInUseCournt(ids);
		if(cournts.size()>0){
			renderRespondJson(ReturnCode.ErrorCode,"记录中包含使用中的信息");
		}
		Boolean flag = DrCourtService.betchUpdateStatus(ids,DrCourt.DISABLE,getLoginUser(),request.remoteAddress);
		if(flag)
			renderRespondJson(ReturnCode.SuccessCode,"禁用成功");
		renderRespondJson(ReturnCode.ErrorCode,"禁用失败");
	}
	/**
	 * 更改法庭信息
	 * @param drCourt
	 * @param phones
	 */
	public static void mainstianCourt(DrCourt drCourt,DrMicrophone[] phones){
		Boolean flag ;
		valiData(drCourt);
		Integer microPhoneCount = 0;
		if(phones==null||phones.length<=0){
			renderRespondJson(ReturnCode.ErrorCode,"麦克风不可为空");
		}
		/**
		 * 根据是否包含法庭id来判断是添加还是更新
		 */
		if(StringUtil.isEmpty(drCourt.getCourtid())){
			DrCourt oldCourt=DrCourtService.getByName(drCourt.getCourtname());
			if(oldCourt!=null){
				renderRespondJson(ReturnCode.ErrorCode,"该法庭已存在");
			}
			flag = DrCourtService.addCourt(drCourt,getLoginUser(),request.remoteAddress,phones);
		}else{
			flag = DrCourtService.updateCourt(drCourt,getLoginUser(),request.remoteAddress,phones);
		}
		if(flag)
			renderRespondJson(ReturnCode.SuccessCode,microPhoneCount.toString());
		renderRespondJson(ReturnCode.ErrorCode,"服务器开小差了~");
	}
	
	/**
	 * 表单验证
	 * @param drCourt
	 */
	private static void valiData(DrCourt drCourt){
		if(drCourt == null){
			renderRespondJson(ReturnCode.ErrorCode,"要添加数据为空");
		}
		if(StringUtil.isEmpty(drCourt.getCourtname().trim())){
			renderRespondJson(ReturnCode.ErrorCode,"法庭名称为空");
		}
		if(StringUtil.isEmpty(drCourt.getRemoteurl().trim())){
			renderRespondJson(ReturnCode.ErrorCode,"麦克风地址为空");
		}
		if(drCourt.getVadhead()==null){
			renderRespondJson(ReturnCode.ErrorCode,"首部静音单位为空");
		}
		if(drCourt.getVadhead().compareTo(new BigDecimal(0)) == -1  || drCourt.getVadhead().compareTo(new BigDecimal(1800000)) == 1 ){
			renderRespondJson(ReturnCode.ErrorCode,"首部静音单位须在0-1800000秒之间");
		}
		if(drCourt.getVadseg()==null){
			renderRespondJson(ReturnCode.ErrorCode,"尾部静音单位为空");
		}
		if(drCourt.getVadseg().compareTo(new BigDecimal(0)) == -1  || drCourt.getVadseg().compareTo(new BigDecimal(30000)) == 1 ){
			renderRespondJson(ReturnCode.ErrorCode,"尾部静音单位须在0-30000秒之间");
		}
		if(drCourt.getMaxsecond()==null){
			renderRespondJson(ReturnCode.ErrorCode,"最大的声音长度为空");
		}
		if(drCourt.getMaxsecond().compareTo(new BigDecimal(0)) == -1  || drCourt.getMaxsecond().compareTo(new BigDecimal(60)) == 1 ){
			renderRespondJson(ReturnCode.ErrorCode,"最大的声音长度须在0-60秒之间");
		}
		
	}
}
